//
//  LoginSonViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/23.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "LoginSonViewController.h"
#import "LoginHeaderTableViewCell.h"
#import "LoginLeftViewController.h"
#import "UIImageView+WebCache.h"
#import "LoginFourTableViewCell.h"
#import "EditLoginTableViewCell.h"
#import "PersonalViewController.h"
#import "DecorateViewController.h"
#import "CollectingViewController.h"
@interface LoginSonViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;

@end

@implementation LoginSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(clickRight:)];
    
    self.arr = [NSMutableArray arrayWithObjects:@"推荐给好友", @"去AppStore评价", @"清理缓存", @"关于美好生活", @"消息中心", @"我喜欢的", nil];
    [self creatTableView];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"LoginHeaderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"LoginFourTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"fourCell"];
    [_tableView registerClass:[EditLoginTableViewCell class] forCellReuseIdentifier:@"editCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 90;
    }  else if (indexPath.section == 1) {
        return 40;
    } else {
        return 45 ;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        // 个人详情
        PersonalViewController *personVC = [[PersonalViewController alloc] init];
        personVC.navigationController.navigationBar.hidden = YES;
        [self.navigationController pushViewController:personVC animated:YES];
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        //清除缓存
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"是否清理缓存？"delegate:self cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定", nil];
        alertView.tag = 1011;
        [alertView show];
        
    } else if (indexPath.section == 2) {
        // 退出登录
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user removeObjectForKey:@"dic"];
        [self.navigationController popToRootViewControllerAnimated:YES];
        [_tableView reloadData];
        
    } else if (indexPath.section == 1 && indexPath.row == 4) {
        NSLog(@"私信");
    } else if (indexPath.section == 1 && indexPath.row == 5) {
        CollectingViewController *collectionVC = [[CollectingViewController alloc] init];
        [self.navigationController pushViewController:collectionVC animated:YES];
    } else {
        NSLog(@"此功能暂未开放");
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 6;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LoginHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        _dic = [user objectForKey:@"dic"];
        NSDictionary *messageDic = [_dic objectForKey:@"user"];
        NSString *strName = [messageDic objectForKey:@"userName"];
        cell.nameLabel.text = strName;
        NSDictionary *imageDic = [messageDic objectForKey:@"userImage"];
        NSString *strImage = [imageDic objectForKey:@"large"];
        cell.nameImage.layer.cornerRadius = 30;
        cell.nameImage.layer.masksToBounds = YES;
        [cell.nameImage sd_setImageWithURL:[NSURL URLWithString:strImage] placeholderImage:[UIImage imageNamed:@"占位图"]];
        return cell;
    } else if (indexPath.section == 1) {
        LoginFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fourCell"];
        cell.label.text = _arr[indexPath.row];
        return cell;
    } else {
        EditLoginTableViewCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell"];
        return editCell;
    }
    return nil;
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 && alertView.tag == 1011) {
        [self clearCache];
    }
}

//清理缓存
-(void) clearCache {
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess)
                                              withObject:nil waitUntilDone:YES];});
}

-(void)clearCacheSuccess
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"缓存清理成功！"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
    [alertView show];
}

- (void)clickRight:(UIBarButtonItem *)sender {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [user objectForKey:@"dic"];
    
    if (dic != nil) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        NSLog(@"请登录");
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
